package cn.wolfcode.car.business.web.controller.system;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQueryObject;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 结算单列表控制器
 business.web.controller.system.StatementItemController
 */
@Controller
@RequestMapping("system/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/system/statementItem/";

    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;


    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:statementItem:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("system:statementItem:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("system:statementItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("statementItem", statementItemService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    ///system/statementItem/query?statementId="+statementId,
    @RequiresPermissions("system:statementItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQueryObject qo,Long statementId){
        return statementItemService.query(qo,statementId);
    }




    //删除
    @RequiresPermissions("system:statementItem:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        statementItemService.deleteBatch(ids);
        return AjaxResult.success();
    }



    //itemDetail跳转结算列表
    @RequestMapping("/itemDetail")
    @RequiresPermissions("system:statementItem:itemDetail")
    public String itemDetail(Long statementId,Model model){
        //判断当前数据是否是已付费,付费状态直接去查询页面
        if (Statement.STATUS_PAID.equals(statementService.getStatement(statementId).getStatus())){
            Statement statement = statementService.get(statementId);
            //我们通过查看前端页面,发现`需要显示收款人信息,所哟我们需要拿到并且封装进去
            User user = userService.get(statement.getPayeeId());
            statement.setPayee(user);
            //保存进去送到页面;
            model.addAttribute("statement",statement);
            return prefix +"showDetail";
        }
        //跳转到列表明细页面
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement",statement);
        return prefix +"itemDetail";
    }

       //saveItems保存操作
    @RequestMapping("/saveItems")
    @RequiresPermissions("system:statementItem:saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> statementItems){
        statementItemService.saveItems(statementItems);
        return AjaxResult.success();
    }

    //支付按钮payStatement
    @RequestMapping("/payStatement")
    @RequiresPermissions("system:statementItem:payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId){
        //支付成功去修改状态
        statementService.updateByStart(statementId);
        return AjaxResult.success();
    }
}
